//
// Created by LiuYou on 2021/10/24.
//

#include <iostream>
#include <unistd.h>
#include <csignal>
#include <cstring>
#include "AnyTimer.hpp"


static bool isLoop = true;


/*
 * 例如: func1((void*) "GO" );
 */
static void func1(void* ptr) {
    printf( __FUNCTION__ );
    printf( "(): %s\n", (const char*) ptr );
}

static void func2(void* ptr) {
    printf( __FUNCTION__ );
    printf( "(): %s\n", (const char*) ptr );
}

static void func3(void* ptr) {
    printf( "func3();执行了。\n" );
}


static void interruptHandler(int signal) {
    isLoop = false;
}


int main(int argc, char** argv) {

    signal( SIGINT, interruptHandler );



    /*
     * 希望5秒钟后调用 func1();
     * 希望2钟之后调用 func2();
     * 希望7秒钟之后调用 func1();
     */

    puts( "Begin" );


    int job1 = addJob( 5, func1, (void*) "aaa" );
    if (job1 < 0) {
        fprintf( stderr, "addJob();出错了。\n" );
        exit( 1 );
    }
    int job2 = addJob( 2, func2, (void*) "bbb" );
    if (job2 < 0) {
        fprintf( stderr, "addJob();出错了。\n" );
        exit( 1 );
    }
    int job3 = addJob( 7, func1, (void*) "ccc" );
    if (job3 < 0) {
        fprintf( stderr, "addJob();出错了。\n" );
        exit( 1 );
    }

    int job4 = addJob( 15, func3, nullptr );
    if (job4 < 0) {
        fprintf( stderr, "addJob();出错了。\n" );
        exit( 1 );
    }


    puts( "end" );


    int count = 0;
    char countStr[10]{};
    while (isLoop) {
        sprintf( countStr, "%d", count );
        write( STDOUT_FILENO, countStr, 3 );
        ++count;
        memset( countStr, '\0', 3 );
        sleep( 1 );
    }


    return 0;
}
